#include "process.h"
#define TARGET_SIZE 1024*1024//1MB

#define DSIZE 1024*10

void download_1()
{
  int target = TARGET_SIZE;
  int total = 0;

  while(total < target)
  {
    usleep(STIME); //用简单的休眠时间，模拟本轮下载花费的时间
    total += DSIZE;
    process_v2(total*100/target);
  }
  printf("\n");
}




//结合场景来谈
void download_2_1(callback_t cd)
{
  int target = TARGET_SIZE;
  int total = 0;

  while(total < target)
  {
    usleep(STIME); //用简单的休眠时间，模拟本轮下载花费的时间
    total += DSIZE;
    int rate = total*100/target;
    cd(rate);
   }
  printf("\n");
}





//优化
void download_2_2(callback_t_1 c)
{
  int testcnt = 100;
  int target = TARGET_SIZE;
  int total = 0; 

  while(total <= target)
  {
    usleep(STIME); //用简单的休眠时间，模拟本轮下载花费的时间
    total += DSIZE;
    double rate = total*100.0/target;
    if(rate > 50.0 && testcnt)
    {
      total = target/2;
      testcnt--;
    }
    c(rate);
   }
   c(100);
  printf("\n");
}




int main()
{
 // int rate = 0;
 // while(rate <= 100)
 // {
 //   process_v2(rate++);
 //   printf("\r");
 //   usleep(12345);
 // }

 // printf("\n");
  
  //下载的软件
  // download_1();
    download_2_2(process_v3);
  return 0;
}
